﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once
#include "GcmpDatabaseInterface.h"
#include "GbmpGuid.h"
#include "GbmpTypeDefs.h"
#include "GbmpQuickCast.h"
#include <cstdint>

namespace gcmp
{
    class IIODevice;
    class ElementId;
    class RawMemory;

    /// \brief 所有I/O流的基类
    class GCMP_DATABASE_INTERFACE_EXPORT IIOStream
    {
        DEFINE_CAST_BASE(gcmp::IIOStream)
    public:
        /// \brief 创建I/O流指针 
        /// \param pIIODevice 输入输出字节流
        /// \return OwnerPtr<IIOStream> I/O流指针
        static OwnerPtr<IIOStream> CreateIOStream(IIODevice* pIIODevice);

        /// \brief 获取底层实现的NdbIODevice的接口 
        /// \return IIODevice 输入输出字节流指针
        virtual IIODevice * GetDevice() = 0;

        /// \brief 获取读取时底层实现的NdbIODevice的接口 
        /// \return IIODevice 输入输出字节流指针
        virtual const IIODevice * GetDeviceForRead() const = 0;

        /// \brief 写入指定长度的裸数据 
        /// \param *ptr 写入的数据指针
        /// \param len 写入数据长度
        /// \return IIOStream I/O流指针
        virtual IIOStream & Write(const void *ptr, uint64_t len) = 0;

        /// \brief 读指定长度的裸数据，缓存由外部管理 
        /// \param *ptr 存放读取数据的指针
        /// \param len 读取数据长度
        /// \return IIOStream I/O流指针
        virtual IIOStream & Read(void *ptr, uint64_t len) = 0;

        /// \brief 读出数据作为std::string
        /// \param value 读出的std::string放在value里
        /// \return uint64_t 以字节计的读出的std::string的长度；-1表示读取失败
        virtual uint64_t ReadString(std::string &value) = 0;

        /// \brief 读出数据作为std::wstring
        /// \param value 读出的std::wstring放在value里
        /// \return uint64_t 以字节计的读出的std::wstring的长度；-1表示读取失败
        virtual uint64_t ReadString(std::wstring &value) = 0;

        /// \brief 读出string的数据，放到RawMemory中。格式是长度(UIn32)+内容(n * char)。
        /// \param rawMemory 会被重置，以满足存放string数据的要求。
        /// \return uint64_t 以字节计的读出的std::wstring的长度；-1表示读取失败
        virtual UInt64 ReadString(RawMemory & rawMemory) = 0;

        /// \brief 写入std::string
        /// \param value 给定的std::string
        /// \return uint64_t 以字节计的写入的std::string的长度；-1表示写入失败
        virtual uint64_t WriteString(const std::string& value) = 0;

        /// \brief 写入std::wstring
        /// \param value 给定的std::wstring
        /// \return uint64_t 以字节计的写入的std::wstring的长度；-1表示写入失败
        virtual uint64_t WriteString(const std::wstring& value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的float值
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (float value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的double值
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (double value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的char值
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (char value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的unsigned char值
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (unsigned char value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的Guid
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (Guid guid) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的WChar
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (WChar value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的Bool
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (Bool value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的uint16_t
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (uint16_t value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的uint32_t
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (uint32_t value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的uint64_t
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (uint64_t value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的int16_t
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (int16_t value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的int32_t
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (int32_t value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的int64_t
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (int64_t value) = 0;

        /// \brief 输出流操作符重载接口 
        /// \param value 输出的ElementId
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator << (const ElementId & value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输如的float数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (float &value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的double数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (double &value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的char数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (char &value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的unsigned char数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (unsigned char &value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的Guid数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (Guid &guid) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的WChar数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (WChar &value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的Bool数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (Bool& value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的uint16_t数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (uint16_t& value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的uint32_t数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (uint32_t& value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的uint64_t数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (uint64_t& value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的int16_t数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (int16_t& value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的int32_t数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (int32_t& value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的int64_t数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (int64_t& value) = 0;

        /// \brief 输入流操作符重载接口 
        /// \param value 存放输入的ElementId数据
        /// \return IIOStream I/O流指针
        virtual IIOStream& operator >> (ElementId& value) = 0;

        virtual ~IIOStream() = 0;
    };
}

